/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fujiyuu75.sequent.sample;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

/**
 * 简单的页面指示器
 */
public class DotIndicatorController extends DirectionalLayout {

    private int selectedIndicatorColor;
    private int unselectedIndicatorColor;
    private int slideCount;
    private Context context;

    public DotIndicatorController(Context context) {
        this(context, null);
    }

    public DotIndicatorController(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public DotIndicatorController(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        selectedIndicatorColor = 0x383838ff;
        unselectedIndicatorColor = 0xccccccff;
        newInstance();
    }

    private Component newInstance() {
        LayoutConfig newLayoutParams = new LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        newLayoutParams.alignment = LayoutAlignment.VERTICAL_CENTER;
        setLayoutConfig(newLayoutParams);
        setOrientation(HORIZONTAL);
        setAlignment(LayoutAlignment.CENTER);
        return this;
    }

    /**
     * 设置指示器子元素的数量
     *
     * @param slideCount 数量
     */
    public void initialize(int slideCount) {
        this.slideCount = slideCount;
        for (int i = 0; i < slideCount; i++) {
            Component dot = new Component(this.context);
            dot.setBackground(createCircleDrawable(unselectedIndicatorColor, 25));
            LayoutConfig params = new LayoutConfig(
                    30,
                    30);
            params.setMargins(8, 8, 8, 8);
            dot.setLayoutConfig(params);
            if (slideCount == 1) {
                dot.setVisibility(Component.INVISIBLE);
            }
            addComponent(dot, params);
        }
        selectPosition(0);
    }

    /**
     * 设置选中的子元素索引
     *
     * @param index 子元素索引
     */
    public void selectPosition(int index) {
        int tint;
        for (int i = 0; i < slideCount; i++) {
            if (i == index) {
                tint = selectedIndicatorColor;
            } else {
                tint = unselectedIndicatorColor;
            }
            Component imageView = getComponentAt(i);
            imageView.setBackground(createCircleDrawable(tint, 25));
        }
    }

    private ShapeElement createCircleDrawable(int color, float radius) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(ShapeElement.RECTANGLE);
        drawable.setRgbColor(new RgbColor(color));
        drawable.setCornerRadius(radius);
        return drawable;
    }

}